<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-model="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>vue自定义事件内容分发</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

</head>
<body>
<div id="app">
    <todo>
        <todo-title slot="todo-title" :title="title_name"></todo-title>
        <todo-items slot="todo-items" v-for="(i,x) in todoItems" :item="i" :index="x" v-on:remove="removeItems(x)"></todo-items>
    </todo>
</div>
</body>
<script>
    //slot:插槽
    Vue.component("todo",{
        template:
            '<div>' +
                '<slot name="todo-title"></slot>' +
                '<ul>' +
                    '<slot name="todo-items"></slot>' +
                '</ul>' +
            '</div>'
    });
    Vue.component("todo-title",{
        props:['title'],
        template: '<div>{{title}}</div>'
    });
    Vue.component("todo-items",{
        props:['item',"index"],
        //只能绑定当前组件的方法
        template: '<li>{{index}}---{{item}}<button @click="remove">delete</button></li>',
        methods:{
            remove:function (index) {
                this.$emit('remove',index);//emit自定义事件调用v-on:remove，v-on:remove调用removeItems(inde x)
            }
        }
    });
    // 实例化vue对象
    new Vue({
        el:"#app",
        data:{
            "todoItems":['java','php','vue'],
            "title_name":"我就是一个标题"
        },
        methods:{
            removeItems:function (index) {
                this.todoItems.splice(index,1);//一次删除一个元素
            }
        }
    });
</script>
</html>